import { post } from '@feidao-factory/send-msg/browser/request';
import global from '../../core/global';

export interface IUserInfo {
	_id: string;
	usercode: string;
	user_name: string;
	name_spell: string;
	is_disabled: boolean;
	create_time: number;
	productid: string;
	avatar_path: string;
	avatar_name: string;
	avatar_type: string;
	email: string;
	phone: string;
	last_login_time: number;
	phone_verified: boolean;
	email_verified: boolean;
	is_admin: boolean;
	code: number;
	msg: string;
	sessionID: string;
}

/**
 * 用户登录
 * @param usr 用户名
 * @param psw 密码
 * @example user_login(usr,psw);
 */
export default async function user_login(usr: string, psw: string) {
	const msg = {
		psw,
		usr
	};
	try {
		const url = `${global.host}/login-without-captcha`;
		const res = await post(url, JSON.stringify(msg));
		return JSON.parse(res.responseText) as IUserInfo;
	} catch (res) {
		const err = JSON.parse(res.responseText) as { msg: string; detail: string; };
		console.error('登陆失败--错误信息---', res.responseText);
		throw new Error(err.msg);
	}
}
